package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.department_manager;
import com.trxy.hotelsystem.model.staffInf;
import com.trxy.hotelsystem.service.impl.Department_managerServiceImpl;
import com.trxy.hotelsystem.service.impl.StaffyInfserviceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class StaffInfController {
	
	@Autowired
	private StaffyInfserviceImpl staffyInfserviceImpl;
	
	@Autowired
	private Department_managerServiceImpl department_managerServiceImpl;
	
	
	//显示员工表
	@RequestMapping(value = "/queryStaff.do")
	public String queryStaffAll(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<staffInf> list = staffyInfserviceImpl.queryStaffAll();
		
        PageInfo<staffInf> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		
		
		return "view/staffInf";
	}
	
	//查询员工信息回显给模态框
	@RequestMapping(value = "/selectStafBykey.do")
	public void selectStafBykey(HttpServletRequest request,HttpServletResponse response,
			Model model) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
//		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		
		staffInf staf = staffyInfserviceImpl.selectStafBykey(id);
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
		String date = time.format(staf.getHiredate());
		staf.setReserved1(date);
		
		
		if(staf != null) {
			String st = JSONArray.fromObject(staf).toString();
			response.getWriter().write(st);
//			pw.write("1");
			
		}else{
//			pw.write("0");
		}
		
	}
	
	//添加员工
	@RequestMapping(value = "/addStaf.do")
	public String addStaf(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staf) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取表单数据
		String stafName = request.getParameter("stafName");
		String stafgender = request.getParameter("stafgender");
		String stafage = request.getParameter("stafage");
		String stafJob = request.getParameter("stafJob");
		String stafId = request.getParameter("stafId");
		String stafAddress = request.getParameter("stafAddress");
//		String stafJuri = request.getParameter("stafJuri");
		String stafWorktime = request.getParameter("stafWorktime");
		String stafphone = request.getParameter("stafphone");
		String stafRe = request.getParameter("stafRe");
		
		department_manager department_Manager = department_managerServiceImpl.selectBydepartment(stafJob);
		
		//数据装成对象
		staf.setName(stafName);
		staf.setGender(stafgender);
		staf.setAge(Integer.parseInt(stafage));
		staf.setJobtitle(stafJob);
		staf.setIdnumber(stafId);
		staf.setAddress(stafAddress);
		staf.setJurisdiction(department_Manager.getJurisdiction());
		staf.setWorktime(stafWorktime);
		staf.setPhone(stafphone);
		staf.setRemarks(stafRe);
		staf.setId(PrimaryKeyUtil.getPrimaryKey());
		staf.setPassword("000000");
		
		//自动生成员工号
		String str;
		while(true) {
			str =String.valueOf(Math.round(Math.random()*1000000));
			staf.setNumber(str);
			List<staffInf> list = staffyInfserviceImpl.selectStafByNumber(staf);
			if(list.size() == 0) {
				if(str.length() == 6) {
					staf.setNumber(str);
					break;
				}
				
			}
		}
		
		Date date = new Date();
        staf.setHiredate(date);
		
        //访问服务层
		int i = staffyInfserviceImpl.addStaf(staf);
		
		//添加成功与否判断
		if(i > 0) {
			model.addAttribute("title", "成功添加员工");
			model.addAttribute("content1", "欢迎你的加入");
			model.addAttribute("content2", "你的员工号为："+str);
			return "view/addStafSuccess";
		}else {
			return "view/addStfdefeated";
		}
	}
	
	//删除员工信息
	@RequestMapping(value = "/deleteStafById.do")
	public void deleteStafById(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staf) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		staf.setId(id);
		
		int i = staffyInfserviceImpl.deleteStafById(staf);
		
		if(i > 0) {
			
			pw.write("1");
		}else {
			
			pw.write("0");
		}
		
	}
	
	
	//通过员工号查询员工信息
	@RequestMapping(value = "selectStfByNumber.do")
	public String selectStfByNumber(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String number = request.getParameter("number");
		staff.setNumber(number);
		
		List<staffInf> list = staffyInfserviceImpl.selectStafByNumber(staff);

		PageInfo<staffInf> page = new PageInfo<>(list);

		model.addAttribute("pageInfo", page);
		
		return "view/staffInf";
	}
	
	//通过员工姓名查询员工
	@RequestMapping(value = "/selectStafByname.do")
	public String selectStafByname(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String stafName = request.getParameter("stafName");
		
		List<staffInf> list = staffyInfserviceImpl.selectStafByName(stafName);
		
		PageInfo<staffInf> page = new PageInfo<>(list);

		model.addAttribute("pageInfo", page);
		
		return "view/staffInf";
	}
	
	//修改员工信息
	@RequestMapping(value = "updataStaf.do")
	public void updataStaf(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		//获取参数
		String stafNumber = request.getParameter("stafNumber");
		String stafName = request.getParameter("stafName");
		String stafIdNumber = request.getParameter("stafIdNumber");
		String staAddress = request.getParameter("staAddress");
		String stafJobtitle = request.getParameter("stafJobtitle");
		String stafGender = request.getParameter("stafGender");
		String stafAge = request.getParameter("stafAge");
		String stafWorktime = request.getParameter("stafWorktime");
		String stafPhone = request.getParameter("stafPhone");
		String remarks = request.getParameter("remarks");
		
		//装箱
		staff.setNumber(stafNumber);
		staff.setName(stafName);
		staff.setIdnumber(stafIdNumber);
		staff.setAddress(staAddress);
		staff.setJobtitle(stafJobtitle);
		staff.setGender(stafGender);
		staff.setAge(Integer.parseInt(stafAge));
		staff.setWorktime(stafWorktime);
		staff.setPhone(stafPhone);
		staff.setRemarks(remarks);
		
		int i = staffyInfserviceImpl.updataStaf(staff);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}	
	}
	
	//查询全部职称权限
	@RequestMapping(value = "selectdepartment_managerAll.do")
	public String selectdepartment_managerAll(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		List<department_manager> deplist = department_managerServiceImpl.selectdepartment_managerAll();
		
		model.addAttribute("deplist", deplist);
		
		return "view/addStaf";
	}
	
	//员工修改密码
	@RequestMapping(value = "updateStafpassword.do")
	public void updateStafpassword(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff,HttpSession session) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String password1 = request.getParameter("password1");
		String password3 = request.getParameter("password3");
		
		staffInf us = (staffInf) session.getAttribute("loginUser");
		
		staff.setPassword(password3);
		staff.setNumber(us.getNumber());
		
		if(password1.equals(us.getPassword())) {
			int i = staffyInfserviceImpl.updataStaf(staff);
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
		}else {
			pw.write("2");
		}
	}
	
	//员工个人中心
	@RequestMapping(value = "personalCenter.do")
	public String personalCenter(HttpServletRequest request,HttpServletResponse response,
			Model model,staffInf staff,HttpSession session) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		staffInf us = (staffInf) session.getAttribute("loginUser");
		
		staffInf staflist = staffyInfserviceImpl.selectStafBykey(us.getId());
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
		String date = time.format(staflist.getHiredate());
		staflist.setReserved1(date);
		
		model.addAttribute("staflist", staflist);
		
		return "view/staf/updatepassword";
	}
	

}
